﻿using EntityModel;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;

namespace API.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class AdvertisementController : ControllerBase
    {
        private readonly BitDbContext _bit;
        public AdvertisementController(BitDbContext Bit)
        {
            _bit = Bit;
        }
        /// <summary>
        /// 查询表
        /// </summary>
        /// <returns></returns>

        [HttpGet]
        public Result Get()
        {

            Result result = new Result();
            result.Data = _bit.EM_Advertisements.ToList();
            return result;
        }
        /// <summary>
        /// 查找广告ID
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        /// 

        [HttpGet("{id}")]
        public Result GetT(Guid id)
        {

            Result result = new Result();
            result.Data = _bit.EM_Advertisements.Where(w => w.ID == id).ToList();
            return result;
        }




        /// <summary>
        /// 修改广告
        /// </summary>
        /// <param name="act"></param>
        /// <returns></returns>
        [HttpPut]
        public Result UP([FromBody] EM_Advertisement act)
        {
            Result result = new Result();
            _bit.EM_Advertisements.Update(act);
            result.Data = _bit.SaveChanges();
            return result;
        }



        /// <summary>
        /// 删除广告
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete]
        public Result DelUser(Guid id)
        {
            Result result = new Result();
             EM_Advertisement? user = _bit.EM_Advertisements.Where(w => w.ID == id).SingleOrDefault();
            if (user != null)
                _bit.EM_Advertisements.Remove(user);
            result.Data = _bit.SaveChanges();
            return result;
        }
        /// <summary>
        /// 增加广告
        /// </summary>
        /// <param name="ware"></param>
        /// <returns></returns>
        [HttpPost]
        public Result AddWare([FromBody] EM_Advertisement ware)
        {
            Result result = new Result();
            _bit.EM_Advertisements.Add(ware);
            result.Data = _bit.SaveChanges();
            return result;
        }


    }
}
